﻿using System.Windows;
using System.Windows.Controls;

namespace FluentTemplates.Test.FluentControls
{
    [TemplatePart(Name = ElementNames.NameBox, Type = typeof(TextBox))]
    [TemplatePart(Name = ElementNames.SubmitButton, Type = typeof(Button))]
    public class TestFluentControl : Control, ITemplatePartBinding
    {
        #region Fields

        protected TextBox nameBox;
        protected Button submitButton;

        #endregion

        #region C'tors

        public TestFluentControl()
        {
            DefaultStyleKey = typeof(TestFluentControl);
        }

        #endregion

        #region Instance Methods

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (DesignTools.IsInDesignModeStatic)
                return;

            nameBox = this.GetTemplateChild<TextBox>(ElementNames.NameBox)
                        .Required()
                        .WireUp(x => x.BindingValidationError += BoxValidationError);

            submitButton = GetTemplateChild(ElementNames.SubmitButton)
                            .As<Button>()
                            .DependsOn(nameBox)
                            .WireUp(b => b.Click += (sender, e) => RunSave());
        }

        private void RunSave()
        {
        }

        #endregion

        #region Event Handling

        private void BoxValidationError(object sender, ValidationErrorEventArgs e)
        {
        }

        #endregion

        #region Nested type: ElementNames

        private static class ElementNames
        {
            #region Constants

            public const string NameBox = "NameBox";
            public const string SubmitButton = "SubmitButton";

            #endregion
        }

        #endregion

        public object GetTemplatePartBinding(string partName)
        {
            throw new System.NotImplementedException();
        }

        public TemplatePart<T> GetTemplatePartBinding<T>(string partName) where T : DependencyObject
        {
            return new TemplatePart<T>(GetTemplateChild(partName) as T, partName, this);
        }
    }
}